package com.yanqu.road.server.cmd.union.cross.honors;

import com.yanqu.road.entity.union.honors.UnionHistoryHonorsRecordData;
import com.yanqu.road.pb.union.cross.CrossUnionHonorsProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.C_UNION_GET_UNION_HISTORY_HONORS_RECORD, desc = "获取历史荣誉")
public class CrossGetUnionHistoryHonorsRecordCmd extends CrossCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();

        if(context.getRet() != 0){
            return;
        }

        CrossUnionHonorsProto.GetUnionHistoryHonorsRecordRespMsg.Builder respMsg = CrossUnionHonorsProto.GetUnionHistoryHonorsRecordRespMsg.newBuilder();
        respMsg.setRet(0);
        List<UnionHistoryHonorsRecordData> list = CrossUnionHonorsMgr.getHistoryHonorsRecordList(context.getUnionInfo().getUnionUid());
        if(list != null){
            for (UnionHistoryHonorsRecordData record : list) {
                if(record.getHonors() <= 0){
                    //荣誉点0的不显示（后台操作扣了荣誉点）
                    continue;
                }
                CrossUnionHonorsProto.UnionHistoryHonorsRecordTemp.Builder builder = CrossUnionHonorsProto.UnionHistoryHonorsRecordTemp.newBuilder();
                builder.setActivityChildType(record.getActivityChildType());
                builder.setActivityType(record.getActivityType());
                builder.setGetTime(record.getGetTime());
                builder.setHonors(record.getHonors());
                builder.setRank(record.getRank());
                builder.addAllServerList(StringUtils.stringToLongList(record.getServerList(), ","));
                builder.setGoodId(record.getGoodId());
                builder.setActivityId(record.getActivityId());
                if(!StringUtils.isNullOrEmpty(record.getUnionNames())){
                    List<String> names = StringUtils.stringToStringList(record.getUnionNames(), "@#\\$");
                    for (int i = 0; i < names.size() && i < 5; i++) {
                        builder.addUnionName(names.get(i));
                    }
                }
                respMsg.addRecord(builder);
            }
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_GET_UNION_HISTORY_HONORS_RECORD, respMsg));
    }

    @Override
    public void sendData() {

    }
}
